package com.example.slidemenu;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class SlideMenu extends ViewGroup {

    private boolean mFunctionDelete;//删除
    private boolean mFunctionRead;//设为已读
    private boolean mFunctionTop;//置顶
    private int mFunctionNum = 0;//这三个用到的数量

    private float mLastX;
    private float mInterceptDownX;
    private boolean isOpen = false;
    private View mContentView;
    private View mFunctionView;
    private OnFunctionClickListener mOnFunctionClickListener = null;

    public SlideMenu(Context context) {
        this(context, null);
    }

    public SlideMenu(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SlideMenu(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SlideMenu);
        int function = typedArray.getInt(R.styleable.SlideMenu_function, 0);
        setFunction(function);
    }

    private void setFunction(int function) {
        switch  (function) {
            case 0://默认为0
                mFunctionDelete = false;
                mFunctionRead = false;
                mFunctionTop = false;
                mFunctionNum = 0;
                break;
            case 1://delete,1
                mFunctionDelete = true;
                mFunctionRead = false;
                mFunctionTop = false;
                mFunctionNum = 1;
                break;
            case 2://read,10
                mFunctionDelete = false;
                mFunctionRead = true;
                mFunctionTop = false;
                mFunctionNum = 1;
                break;
            case 4://top,100
                mFunctionDelete = false;
                mFunctionRead = false;
                mFunctionTop = true;
                mFunctionNum = 1;
                break;
            case 3://delete|read,11
                mFunctionDelete = true;
                mFunctionRead = true;
                mFunctionTop = false;
                mFunctionNum = 2;
                break;
            case 5://delete|top,101
                mFunctionDelete = true;
                mFunctionRead = false;
                mFunctionTop = true;
                mFunctionNum = 2;
                break;
            case 6://top|read,110
                mFunctionDelete = false;
                mFunctionRead = true;
                mFunctionTop = true;
                mFunctionNum = 2;
                break;
            case 7://top|read|delete,111
                mFunctionDelete = true;
                mFunctionRead = true;
                mFunctionTop = true;
                mFunctionNum = 3;
                break;
        }
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mContentView = getChildAt(0);
        //这里已经自动添加SlideMenu包含的子控件(这时只有一个chlid)，还要手动添加自己要的
        mFunctionView = LayoutInflater.from(getContext()).inflate(R.layout.item_slide_function, this, false);//false返回第一个参数的View，true返回this
        this.addView(mFunctionView);//手动绑定
        //这时有两个children
        TextView readTextView = mFunctionView.findViewById(R.id.slide_read);
        TextView topTextView = mFunctionView.findViewById(R.id.slide_top);
        TextView deleteTextView = mFunctionView.findViewById(R.id.slide_delete);
        if (!mFunctionRead) {
            readTextView.setVisibility(GONE);
        }
        if (!mFunctionTop) {
            topTextView.setVisibility(GONE);
        }
        if (!mFunctionDelete) {
            deleteTextView.setVisibility(GONE);
        }
        readTextView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                close();
                mOnFunctionClickListener.onReadClickListener(v);
            }
        });
        topTextView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                close();
                mOnFunctionClickListener.onTopClickListener(v);
            }
        });
        deleteTextView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                close();
                mOnFunctionClickListener.onDeleteClickListener(v);
            }
        });
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);//this的高size
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);//this的宽size
        //测量SlideMenu包含的一个孩子(content)，宽跟this一样，高(1，指定高；2，包裹；3匹配
        LayoutParams contentLayoutParams = mContentView.getLayoutParams();
        int contentHeight = contentLayoutParams.height;
        int contentHeightMeasuredSpec;
        if (contentHeight == LayoutParams.WRAP_CONTENT) {//包裹内容
            contentHeightMeasuredSpec = MeasureSpec.makeMeasureSpec(heightSize, MeasureSpec.AT_MOST);
        } else if (contentHeight == LayoutParams.MATCH_PARENT) {//匹配父控件
            contentHeightMeasuredSpec = MeasureSpec.makeMeasureSpec(heightSize, MeasureSpec.EXACTLY);
        } else {//指定大小
            contentHeightMeasuredSpec = MeasureSpec.makeMeasureSpec(contentHeight, MeasureSpec.EXACTLY);
        }
        mContentView.measure(widthMeasureSpec, contentHeightMeasuredSpec);//与this同宽
        int contentMeasuredHeight = mContentView.getMeasuredHeight();//content的高度

        //测量function部分，3个Function按钮都有的话宽为this的3/5（一个按钮为this宽的1/5），与content同高
        int functionWidth = 0;
        functionWidth = widthSize * mFunctionNum / 5;
        mFunctionView.measure(MeasureSpec.makeMeasureSpec(functionWidth, MeasureSpec.EXACTLY),
                MeasureSpec.makeMeasureSpec(contentMeasuredHeight, MeasureSpec.EXACTLY));


        //测量自己
        setMeasuredDimension(widthSize, contentMeasuredHeight);//这里是宽高

    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        //摆放content
        int contentLeft = 0;
        int contentTop = 0;
        int contentRight = contentLeft + mContentView.getMeasuredWidth();
        int contentBottom = contentTop + mContentView.getMeasuredHeight();
        mContentView.layout(contentLeft, contentTop, contentRight, contentBottom);

        //摆放function
        if (mFunctionNum > 0) {
            int functionLeft = contentRight;
            int functionTop = contentTop;
            int functionRight = functionLeft + mFunctionView.getMeasuredWidth();
            int functionBottom = functionTop + mFunctionView.getMeasuredHeight();
            mFunctionView.layout(functionLeft, functionTop, functionRight, functionBottom);
        }
    }

    //点击事件与touch事件冲突处理
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        //如果是横向滑动就拦截
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mInterceptDownX = ev.getX();
                break;
            case MotionEvent.ACTION_MOVE:
                if (Math.abs(ev.getX() - mInterceptDownX) > 0) {
                    mLastX = ev.getX();
                    return true;
                }
                break;
        }
        return super.onInterceptTouchEvent(ev);//如果全都返回true就只能滑动，不能点击
    }

    //(除了getScrollX()、scrollTo、scrollBy滚动，也可以用改contentLeft和用requestLayout来实现，或者setTranslationY
    /*if (mContentLeft + dX <= 0 && mContentLeft + dX >= -mFunctionView.getMeasuredWidth()) {
                    mContentLeft += dX;
                    requestLayout();//重新布局
                }*/
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                mLastX = event.getX();
                break;
            case MotionEvent.ACTION_MOVE:
                int dX = (int) (event.getX() - mLastX);
                mLastX = event.getX();
                int scrollX = getScrollX() - dX;//移动之后的位置
                //Log.d("slidemenu", scrollX + "");
                if (scrollX >= -50 && scrollX <= mFunctionView.getMeasuredWidth() + 50) {
                    scrollBy(-dX, 0);
                }
                break;
            case MotionEvent.ACTION_UP:
                //回弹
                if (isOpen == false) {//处于关闭状态
                    if (getScrollX() < mFunctionView.getMeasuredWidth() / 3) {
                        close();
                    } else {
                        open();
                    }
                } else {//处于打开状态
                    if (getScrollX() > mFunctionView.getMeasuredWidth() * 2 / 3) {
                        open();
                    } else {
                        close();
                    }
                }
                break;
        }
        return true;
    }

    public boolean isOpen() {
        return isOpen;
    }

    public void close() {
        scrollTo(0, 0);
        isOpen  = false;
    }

    public void open() {
        scrollTo(mFunctionView.getMeasuredWidth(), 0);
        isOpen = true;
    }

    public void setOnFunctionClickListener(OnFunctionClickListener listener) {
        this.mOnFunctionClickListener = listener;
    }

    public interface OnFunctionClickListener {
        void onReadClickListener(View v);
        void onTopClickListener(View v);
        void onDeleteClickListener(View v);
    }
}
